package org.future.wsdl.data;

import java.util.ArrayList;
import java.util.List;

/**
 * wsdl:service 元素
 * @author dycc
 *
 */
public class WsdlService {
    private String name;
    
    private String namespace;
    
    private String targetNamespace;
    
    private List<WsdlPort> ports = new ArrayList<WsdlPort>();
    
    // name
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    // namespace
    public String getNamespace() {
        return namespace != null ? namespace : targetNamespace;
    }
    public void setNamespace(String namespace) {
        this.namespace = namespace;
    }
    // targetNamespace
    public String getTargetNamespace() {
        return targetNamespace;
    }
    public void setTargetNamespace(String targetNamespace) {
        this.targetNamespace = targetNamespace;
    }
    // ports
    public List<WsdlPort> getPorts() {
        return ports;
    }
    public void setPorts(List<WsdlPort> ports) {
        this.ports = ports;
    }
    // wsdl_location
    public String getWsdlLocation(){
        String address = this.getPorts().get(0).getAddress();
        // axis2 发布的服务以 '/' 结尾
        if(address.endsWith("/")){
            int from = address.indexOf("services/");
            int end = address.lastIndexOf(".");
            if(end > from ){
                address = address.substring(0, end);
            }
        }
        address += "?wsdl";
        return address;
    }
    
    // toString
    public String toString(){
        StringBuffer sb = new StringBuffer();
        sb.append("<wsdl:service name=\"" + name + "\"");
        sb.append(">");
        sb.append("\n");
        // add port
        for(int i=0;i<ports.size();i++){
            sb.append(ports.get(i));
            sb.append("\n");
        }
        sb.append("</wsdl:service>");
        return sb.toString();
    }
}
